package org.orz.cloud.common.result;

import org.orz.cloud.common.exception.ErrorCode;
import org.orz.cloud.common.exception.SystemErrorCode;

/**
 * 通用响应类
 * @param <T> data泛型
 */
public class ResultDTO<T> {
    /**
     * 是否成功 true成功 false失败
     */
    private Boolean success;
    /**
     * 响应代码
     */
    private String code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 响应参数
     */
    private T data;

    public ResultDTO() {
        this.success = true;
        this.code = "0";
    }

    public ResultDTO(T data) {
        this.success = true;
        this.code = "0";
        this.data = data;
    }

    public ResultDTO(Boolean success, String code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResultDTO<T> success() {
        return new ResultDTO<T>();
    }

    public static <T> ResultDTO<T> success(T data) {
        return new ResultDTO<T>(data);
    }

    public static <T> ResultDTO<T> fail(ErrorCode code) {
        return new ResultDTO<T>(false, code.getCode(), code.getMessage(),null);
    }

    public static <T> ResultDTO<T> fail(T data){
        return new ResultDTO<T>(false, SystemErrorCode.BUSINESS_ERROR.getCode(),
                SystemErrorCode.BUSINESS_ERROR.getMessage(), data);
    }

    public static <T> ResultDTO<T> fail(ErrorCode code, T data){
        return new ResultDTO<T>(false,code.getCode(),code.getMessage(),data);
    }

    public static <T> ResultDTO<T> fail(String code,String message,T data){
        return new ResultDTO<T>(false,code,message,data);
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
